package com.mall.equipment.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.mall.equipment.entity.BackEntity;
import com.mall.equipment.entity.Person;
import com.mall.equipment.service.EquipmentService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Slf4j
public class EquipmentServiceImpl implements EquipmentService {

    private static final String URL = "http://192.168.110.49:8090/";
    private static final String PASS = "123456";
    private static final String PERSON_URL = URL+"person/";
    private static final String urlCheck = "((http|ftp|https)://)(([a-zA-Z0-9._-]+.[a-zA-Z]{2,6})|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(:[0-9]{1,5})*(/[a-zA-Z0-9&%_./-~-]*)?";

    private Map<String,Object> getMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("pass",PASS);
        return map;
    }

    @Override
    public BackEntity setIdentifyCallBack() {
        Map<String,Object> map = this.getMap();
        String callbackUrl="http://192.168.110.141:8090/equipment/back";
        if(!StrUtil.isEmptyIfStr(callbackUrl) && !ReUtil.isMatch(urlCheck,callbackUrl)){
            return new BackEntity();
        }
        //心跳回调
        callbackUrl="";
        map.put("url",callbackUrl);
        map.put("interval","10");
        String str = HttpUtil.post(URL+"setDeviceHeartBeat",map);
        log.info(str);
        map.remove("interval");
        map.remove("url");

        //识别回调
        map.put("callbackUrl",callbackUrl);
        str = HttpUtil.post(URL+"setIdentifyCallBack",map);
        log.info(str);
        map.remove("callbackUrl");

        //注册照片回调
        map.put("url",callbackUrl);
        str = HttpUtil.post(URL+"setImgRegCallBack",map);
        log.info(JSONUtil.toJsonStr(str));
        map.remove("url");

        //任务获取地址
        map.put("url",callbackUrl);
        str = HttpUtil.post(URL+"setTaskInterfaceAddress",map);
        log.info(JSONUtil.toJsonStr(str));

        //任务处理结果上报地址设置
        map.put("url",callbackUrl);
        str = HttpUtil.post(URL+"setTaskProcessingResultsAddress",map);
        log.info(JSONUtil.toJsonStr(str));
        map.remove("url");

        return JSONUtil.toBean(str,BackEntity.class);
    }


    @Override
    public String getDeviceKey() {
        Map<String,Object> map = this.getMap();
        map.put("personId",1);
        map.put("url","http://192.168.110.141:8090/equipment/back");
        return HttpUtil.post(URL+"getDeviceKey",map);
    }

    @Override
    public String setMaskConfig(int isMaskOpen) {
        Map<String,Object> map = this.getMap();
        map.put("isMaskOpen",isMaskOpen);
        map.put("isVoiceOpen",isMaskOpen);
        map.put("voiceContext","");
        String str = HttpUtil.post(URL+"setMaskConfig",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(str);
        return back.getMsg();
    }

    @Override
    public String openDoor() {
        Map<String,Object> map = this.getMap();
        String str = HttpUtil.post(URL+"device/openDoorControl",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(str);
        return back.getMsg();
    }
    @Override
    public String callBack() {
        Map<String,Object> map = this.getMap();
        String str = HttpUtil.get(URL+"/device/callback",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        return back.getData();
    }

    @Override
    public BackEntity setTime() {
        Map<String,Object> map = this.getMap();
        //毫秒级时间戳
        long timestamp = new Date().getTime();
        map.put("timestamp",timestamp);
        String str = HttpUtil.post(URL+"setTime",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        return back;
    }

    @Override
    public BackEntity createPerson(Person person) {
        Map<String,Object> map = this.getMap();
        map.put("person",JSONUtil.toJsonStr(person));
        String str = HttpUtil.post(PERSON_URL+"create",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        if(back.getResult()==1){
            Person p = JSONUtil.toBean(back.getData(),Person.class);
        }
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity delPerson(String id) {
        Map<String,Object> map = this.getMap();
        map.put("id",id);
        String str = HttpUtil.post(PERSON_URL+"delete",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity updatePerson(Person person) {
        Map<String,Object> map = this.getMap();
        if(StrUtil.isEmptyIfStr(person.getId()) || StrUtil.isEmptyIfStr(person.getName())){
            return new BackEntity();
        }
        map.put("person",JSONUtil.toJsonStr(person));
        String str = HttpUtil.post(PERSON_URL+"update",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        if(back.getResult()==1){
            Person p = JSONUtil.toBean(back.getData(),Person.class);
        }
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity createPasstime(String personId, String time) {
        Map<String,Object> map = this.getMap();
        Map<String,Object> personMap = new HashMap<>();
        personMap.put("personId",personId);
        personMap.put("passtime",time);
        map.put("passtime",JSONUtil.toJsonStr(personMap));
        String str = HttpUtil.post(PERSON_URL+"createPasstime",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);

        //批量修改
        /*Map<String,Object> map = this.getMap();
        Map<String,Object> personMap = new HashMap<>();
        map.put("personId",personId);
        map.put("passtime",time);
        String str = HttpRequest.put(PERSON_URL+"passtime").form(map).timeout(-1).execute().body();
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);*/

        return back;
    }

    @Override
    public BackEntity deletePasstime(String personId) {
        Map<String,Object> map = this.getMap();
        map.put("personId",personId);
        String str = HttpUtil.post(PERSON_URL+"deletePasstime",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity createPermissions(String personId, String time) {
        Map<String,Object> map = this.getMap();
        map.put("time",time);
        map.put("personId",personId);
        String str = HttpUtil.post(PERSON_URL+"permissionsCreate",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity deletePermissions(String personId) {
        Map<String,Object> map = this.getMap();
        map.put("personId",personId);
        String str = HttpUtil.post(PERSON_URL+"permissionsDelete",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public List<Person> find() {
        Map<String,Object> map = this.getMap();
        map.put("id",-1);
        map.put("taskNo","find");
        map.put("interfaceName","person/find");
        map.put("result",true);
        String str = HttpUtil.get(PERSON_URL+"find",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        List<Person> list = JSONUtil.toList(back.getData(),Person.class);
        log.info(JSONUtil.toJsonStr(back));
        return list;
    }

    @Override
    public BackEntity createImg(String personId){
        Map<String,Object> map = this.getMap();
        map.put("personId",personId);
        map.put("faceId",2);
        String url = "C:\\Users\\Administrator\\Pictures\\12.jpg";
        map.put("imgBase64",getBase64Img(url));
        String str = HttpUtil.post(URL+"face/create",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity deleteImg(String personId){
        Map<String,Object> map = this.getMap();
        map.put("faceId",personId);
        String str = HttpUtil.post(URL+"face/delete",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }

    @Override
    public BackEntity deleteRecords(String personId) {
        Map<String,Object> map = this.getMap();
        map.put("personId",personId);
        map.put("startTime", DateUtil.parseDateTime("2021-11-25 01:05:00"));
        map.put("endTime",0);
        String str = HttpUtil.post(URL+"newDeleteRecords",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }


    @Override
    public BackEntity newFindRecords() {
        Map<String,Object> map = this.getMap();
        map.put("personId",-1);
        map.put("startTime",0);
        map.put("endTime",0);
        map.put("length",-1);
        map.put("index",0);
        String str = HttpUtil.get(URL+"findICRecords",map);
        BackEntity back = JSONUtil.toBean(str,BackEntity.class);
        log.info(JSONUtil.toJsonStr(back));
        return back;
    }




    public String getBase64Img(String url){
        int fileLen = GetFileSize(url);
        if (fileLen <= 0) {
            return null;
        }
        InputStream in = null;
        String encode = null; // 返回Base64编码过的字节数组字符串
        try {
            // 读取图片字节数组
            in = new FileInputStream(url);
            byte[] data = new byte[in.available()];
            in.read(data);
            encode = Base64Utils.encodeToString(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return encode;
    }

    // 获取图片大小
    public int GetFileSize(String filePath) {
        File f = new File(filePath);
        if (f.exists() && f.isFile()) {
            return (int) f.length();
        } else {
            return 0;
        }
    }
}
